
#delimit;
set more off;

cd "C:\....\Replication files";
	
use "ENMI - replication.dta", clear;


replace totalcod=0 if totalcod==.;
 #delimit;
 set more off;
 	
/* package required: ivreg2
http://www.stata-journal.com/software/sj7-4/
jan 26 2016
*/
	gen year_inoffice=year if inc_cab_l==1;
	gen year_lastinoffice=L1.year_inoffice;
		gen timesinceoffice=year-year_lastinoffice;
			replace timesinceoffice=0 if inc_cab_l==1;
	replace timesinceoffice=0 if timesinceoffice==.;
	
 ivregress   2sls pvote
	 pvote_l 
c.GDPgrowth c.GDPgrowth#1.inc_cab_l
moderate c.moderate#1.inc_cab_l 1.inc_cab_l

 majelect timesinceoffice
(c.ensh_mi c.ensh_mi#1.inc_cab_l = totalcod dsle )
 	, vce(cluster edate_numeric) first;

	estimates store twosls_enmi_full;
	estat endogenous;
	
	mat list e(b);
	testnl _b[c.ensh_mi]== _b[c.ensh_mi#1.inc_cab_l]==0;


	
/*	Check to see how closely related the instruments 
	are with the DV and the endogenous IV	*/
	
corr ensh_mi  dsle    if e(sample);
corr ensh_mi    totalcod  if e(sample);
corr pvote   totalcod  if e(sample);
corr pvote  dsle  if e(sample);
corr ensh_mi  dsle  totalcod  if e(sample);
corr pvote  dsle totalcod if e(sample);

	#delimit;
	set more off;
 ivregress  2sls pvote
	 pvote_l 
	  c.GDPgrowth#1.inc_cab_l
	 c.moderate#1.inc_cab_l 

 majelect 
	 (  c.ensh_mi#1.inc_cab_l = totalcod dsle )
	 	if inc_cab_l==1, vce(cluster edate_numeric) first ;
	
	estimates store twosls_enmi_incumbentsonly;
		estat endogenous;
		
	#delimit;
ivregress  2sls  pvote
 c.GDPgrowth 
 moderate
 majelect  pvote_l 
( c.ensh_mi= totalcod dsle )
	if inc_cab_l==0, vce(cluster edate_numeric) ;

	estimates store twosls_enmi_oppositiononly;	
		  	estat endogenous;
			
			
	 #delimit;
 esttab  twosls_enmi_oppositiononly twosls_enmi_incumbentsonly twosls_enmi_full 
 using "figures\Table A19 - 2SLS from economic conditions.rtf", replace 
		b (%10.3f) se scalars("rmse Root Mean Squared Error"  "chi2 \u0935?{\super 2}"  ) 
		obslast star(+ 0.10 * 0.05 ** 0.01 *** 0.001)
		label mtitles ( "Opposition" "Incumbents" "Full") nogaps
		title (\label{})
order(ensh_mi 
1.inc_cab_l#c.ensh_mi
moderate 
1.inc_cab_l#c.moderate
GDPgrowth
1.inc_cab_l#c.GDPgrowth 
1.inc_cab_l 
1.nparty
 majelect federal directpres
 pvote_l _cons)
keep(ensh_mi 1.inc_cab_l 
1.inc_cab_l#c.ensh_mi GDPgrowth 
1.inc_cab_l#c.GDPgrowth 
1.inc_cab_l#c.moderate
moderate  majelect 	

pvote_l _cons )  

 coeflabels(
ensh_mi "ENMI" 
1.inc_cab_l "Incumbent"
1.inc_cab_l#c.ensh_mi "Incumbent X ENMI"
GDPgrowth "GDP Growth Rate" 
1.inc_cab_l#c.GDPgrowth "Incumbent X GDP Growth Rate"
1.inc_cab_l#c.moderate "Incumbent X Moderated Position"
moderate "Moderated Position"

majelect "Majoritarian Election"

pvote_l "% Vote{\sub t-1}"
_cons "Constant")
addnotes("Note: The Dependent variable is percentage vote for the party.");





asdf;
		
